package week02;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class TrafficReducer extends Reducer<Text, TrafficRecord, Text, TrafficRecord> {
    @Override
    protected void reduce(Text key, Iterable<TrafficRecord> values, Reducer<Text, TrafficRecord, Text, TrafficRecord>.Context context) throws IOException, InterruptedException {
        long bytesUpSum = 0 ;
        long bytesDownSum = 0;
        for (TrafficRecord rec : values) {
            bytesUpSum += rec.getBytesUp();
            bytesDownSum +=  rec.getBytesDown();
        }

        context.write(key,new TrafficRecord(key.toString(),bytesUpSum,bytesDownSum));
    }
}
